package com.jiuyin.dianjing.ui.fragment.team;

import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.MatchGameItemModel;
import com.jiuyin.dianjing.ui.fragment.base.BaseFragment;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_MATCH_TYPE_CREATE;
import static com.jiuyin.dianjing.constant.HelperConstant.POSITION_DEFAULT;

/**
 * 游戏列表
 */
public class GameGridFragment extends BaseFragment {
    @BindView(R.id.gv_rg_match_select)
    GridView gvRgMatchSelect;
    SelectGameInterface mSelectGameInterface;
    private int mSelectPosition = POSITION_DEFAULT;
    private CommonAdapter<MatchGameItemModel> mAdapter;
    private String mGameId;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_game_grid;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
    }

    @Override
    public void fetchData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_MATCH_TYPE_CREATE);
        JSONObject jsonObject = new JSONObject(params);
        getApiHelper().appGetMyCompetitionGameList(jsonObject.toString(), data -> {
            mAdapter = new CommonAdapter<MatchGameItemModel>(getContext(), R.layout.item_match_create, data) {
                @Override
                protected void convert(ViewHolder viewHolder, MatchGameItemModel item, int position) {
                    viewHolder.setText(R.id.tv_match_create_name, item.getName());
                    ImageView imageView = viewHolder.getView(R.id.img_match_create_icon);
                    Glide.with(getContext()).load(item.getLogo()).into(imageView);
                    if (position == mSelectPosition) {
                        viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_select_bg);
                    } else {
                        viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_normal_bg);
                    }
                }
            };
            gvRgMatchSelect.setAdapter(mAdapter);
            gvRgMatchSelect.setOnItemClickListener((parent, view, position, id) -> {
                if (mSelectPosition != position) {
                    mSelectPosition = position;
                    mGameId = mAdapter.getItem(position).getGameId();
                } else {
                    mSelectPosition = POSITION_DEFAULT;
                    mGameId = "";
                }
                if (mSelectGameInterface != null) {
                    mSelectGameInterface.onSelect(mGameId);
                }
                mAdapter.notifyDataSetChanged();
            });
        });
    }

    public void setOnSelectInterface(SelectGameInterface onSelect) {
        mSelectGameInterface = onSelect;
    }

    public interface SelectGameInterface {
        void onSelect(String gameId);
    }
}
